package com.cabbagetofu.crowd.service.impl;


import com.cabbagetofu.crowd.entity.po.*;
import com.cabbagetofu.crowd.entity.vo.*;
import com.cabbagetofu.crowd.mapper.*;
import com.cabbagetofu.crowd.service.api.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectPOMapper projectPOMapper;

    @Autowired
    private ProjectItemPicPOMapper projectItemPicPOMapper;

    @Autowired
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Autowired
    private MemberConfirmInfoMapper memberConfirmInfoMapper;

    @Autowired
    private ReturnPOMapper returnPOMapper;

    public List<ProjectPO> getAllProject(){
        return projectPOMapper.selectAllProject();
    }

    /**
     * 将ProjectVO中对应的属性逐个存入对应的数据库表中
     * @param projectVO
     * @param id
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, Integer id) {

        ProjectPO projectPO = new ProjectPO();

        // 将ProjectVO的属性信息拷贝导ProjectPO中
        BeanUtils.copyProperties(projectVO, projectPO);
        // 将MemberId存入值projectPO中
        projectPO.setMemberid(id);

        // 生成创建时间
        String createdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        projectPO.setCreatedate(createdate);

        // status设置为0，表示即将开始
        projectPO.setStatus(0);

        // 保存projectPO至数据库中
        // 这里为了在ProjectPO得到自增的主键，
        // 在mapper的xml文件中对应的insert标签增加了useGeneratedKeys="true" keyProperty="id"的配置
        projectPOMapper.insertSelective(projectPO);

        // 获取ProjectPO自增后的id值
        Integer projectId = projectPO.getId();

        // 保存项目、分类关联关系信息
        // 从projectVO中获取分类信息
        List<Integer> typeIdList = projectVO.getTypeIdList();
        // 将分类信息的值保存到对应的表中
        projectPOMapper.insertTypeRelationship(typeIdList, projectId);

        // 保存项目、标签关联关系信息
        // 从projectVO中获取标签
        List<Integer> tagIdList = projectVO.getTagIdList();
        // 将标签的值保存到对应的表中
        projectPOMapper.insertTagRelationship(tagIdList, projectId);

        // 保存发起项目详情图片信息
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(detailPicturePathList, projectId);

        // 保存项目发起人信息
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(id);
        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        // 保存项目回报信息
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();
        List<ReturnPO> returnPOList = new ArrayList<ReturnPO>();
        for (ReturnVO returnVO : returnVOList) {

            ReturnPO returnPO = new ReturnPO();
            BeanUtils.copyProperties(returnVO, returnPO);
            returnPOList.add(returnPO);
        }
        returnPOMapper.insertReturnPOList(projectId, returnPOList);

        // 保存项目确认信息
        MemberConfirmInfo memberConfirmInfoPO = new MemberConfirmInfo();
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
        BeanUtils.copyProperties(memberConfirmInfoVO, memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(id);
        memberConfirmInfoMapper.insert(memberConfirmInfoPO);

    }

    @Override
    public List<PortalTypeVO> getPortalTypeVO() {

        return projectPOMapper.selectPorTalTypeVOList();
    }


    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {

        // 得到DetailProjectVO对象
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);

        // 根据status 确定status的状态
        Integer status = detailProjectVO.getStatus();
        switch (status){
            case 0:
                detailProjectVO.setStatusText("即将开始");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("众筹失败");
                break;
            default:
                break;
        }

        // 根据deployDate计算lastDay
        String deployDate = detailProjectVO.getDeployDate();

        // 获取当前日期
        Date currentDay = new Date();

        // 把众筹的日期转换为Date类型
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date deployDay = format.parse(deployDate);

            // 获取当前日期的时间戳
            long currentDayTime = currentDay.getTime();

            // 获取众筹日期的时间戳
            long deployDayTime = deployDay.getTime();

            // 通过当前日期时间戳-众筹日期时间戳，得到已经去过的时间
            Integer passDay = (int)(currentDayTime - deployDayTime) / 1000 / 60 / 60 / 24;

            // 用设置的众筹天数-过去的时间 得到剩余时间
            int lastDay = detailProjectVO.getDay() - passDay;

            // 给当前的DetailProjectVO对象设置剩余时间
            detailProjectVO.setLastDay(lastDay);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        // 最后返回完善后的detailProjectVO对象
        return detailProjectVO;
    }
}
